/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.report;

import com.thienloc.omap.entity.BroadcastProduction;
import com.thienloc.omap.session.BroadcastProductionFacade;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class BroadcastAndProductionSummaryBean implements Serializable {

    @EJB
    private BroadcastProductionFacade facade;
    private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
    private Map<String, UserBroadcastAndProductionModel> mobifoneYesterdayMap = new TreeMap<String, UserBroadcastAndProductionModel>();
    private Map<String, UserBroadcastAndProductionModel> vinaphoneYesterdayMap = new TreeMap<String, UserBroadcastAndProductionModel>();
    private Map<String, UserBroadcastAndProductionModel> viettelYesterdayMap = new TreeMap<String, UserBroadcastAndProductionModel>();
    private Map<String, UserBroadcastAndProductionModel> mobifoneThisMonthMap = new TreeMap<String, UserBroadcastAndProductionModel>();
    private Map<String, UserBroadcastAndProductionModel> vinaphoneThisMonthMap = new TreeMap<String, UserBroadcastAndProductionModel>();
    private Map<String, UserBroadcastAndProductionModel> viettelThisMonthMap = new TreeMap<String, UserBroadcastAndProductionModel>();
    //unknowns
    private Map<String, UserBroadcastAndProductionModel> mobifoneYesterdayUnknownMap = new TreeMap<String, UserBroadcastAndProductionModel>();
    private Map<String, UserBroadcastAndProductionModel> vinaphoneYesterdayUnknownMap = new TreeMap<String, UserBroadcastAndProductionModel>();
    private Map<String, UserBroadcastAndProductionModel> viettelYesterdayUnknownMap = new TreeMap<String, UserBroadcastAndProductionModel>();
    private Map<String, UserBroadcastAndProductionModel> mobifoneThisMonthUnknownMap = new TreeMap<String, UserBroadcastAndProductionModel>();
    private Map<String, UserBroadcastAndProductionModel> vinaphoneThisMonthUnknownMap = new TreeMap<String, UserBroadcastAndProductionModel>();
    private Map<String, UserBroadcastAndProductionModel> viettelThisMonthUnknownMap = new TreeMap<String, UserBroadcastAndProductionModel>();

    @PostConstruct
    public void postConstruct() {
        logger.trace("Initializing {}", this.getClass().getSimpleName());
        try {
            Calendar startOfYesterday = Calendar.getInstance();
            startOfYesterday.set(Calendar.HOUR_OF_DAY, 0);
            startOfYesterday.set(Calendar.MINUTE, 0);
            startOfYesterday.set(Calendar.SECOND, 0);

            Calendar endOfYesterday = Calendar.getInstance();
            endOfYesterday.set(Calendar.HOUR_OF_DAY, 23);
            endOfYesterday.set(Calendar.MINUTE, 59);
            endOfYesterday.set(Calendar.SECOND, 59);

            List<BroadcastProduction> broadcastProductionsOfYesterday = facade.findByTime(startOfYesterday.getTime(), endOfYesterday.getTime());
            for (BroadcastProduction broadcastProduction : broadcastProductionsOfYesterday) {
                if (broadcastProduction.getProvider().equals("smsgate_mobifone")) {
                    if (broadcastProduction.getUsername() == null) {
                        addToMap(mobifoneYesterdayMap, broadcastProduction);
                    } else {
                        addToMap(mobifoneYesterdayUnknownMap, broadcastProduction);
                    }
                } else if (broadcastProduction.getProvider().equals("smsgate_vinaphone")) {
                    if (broadcastProduction.getUsername() == null) {
                        addToMap(vinaphoneYesterdayMap, broadcastProduction);
                    } else {
                        addToMap(vinaphoneYesterdayUnknownMap, broadcastProduction);
                    }
                } else if (broadcastProduction.getProvider().equals("smsgate_viettel")) {
                    if (broadcastProduction.getUsername() == null) {
                        addToMap(viettelYesterdayMap, broadcastProduction);
                    } else {
                        addToMap(viettelYesterdayUnknownMap, broadcastProduction);
                    }
                }
            }


            Calendar startOfThisMonth = Calendar.getInstance();
            startOfThisMonth.set(Calendar.DATE, 1);
            startOfThisMonth.set(Calendar.HOUR_OF_DAY, 0);
            startOfThisMonth.set(Calendar.MINUTE, 0);
            startOfThisMonth.set(Calendar.SECOND, 0);

            Calendar endOfThisMonth = Calendar.getInstance();
            startOfThisMonth.set(Calendar.DATE, endOfThisMonth.getActualMaximum(Calendar.DATE));
            endOfThisMonth.set(Calendar.HOUR_OF_DAY, 23);
            endOfThisMonth.set(Calendar.MINUTE, 59);
            endOfThisMonth.set(Calendar.SECOND, 59);

            List<BroadcastProduction> broadcastProductionsOfThisMonth = facade.findByTime(startOfThisMonth.getTime(), endOfThisMonth.getTime());
            for (BroadcastProduction broadcastProduction : broadcastProductionsOfThisMonth) {
                if (broadcastProduction.getProvider().equals("smsgate_mobifone")) {
                    if (broadcastProduction.getUsername() == null) {
                        addToMap(mobifoneThisMonthMap, broadcastProduction);
                    } else {
                        addToMap(mobifoneThisMonthUnknownMap, broadcastProduction);
                    }
                } else if (broadcastProduction.getProvider().equals("smsgate_vinaphone")) {
                    if (broadcastProduction.getUsername() == null) {
                        addToMap(vinaphoneThisMonthMap, broadcastProduction);
                    } else {
                        addToMap(vinaphoneThisMonthUnknownMap, broadcastProduction);
                    }
                } else if (broadcastProduction.getProvider().equals("smsgate_viettel")) {
                    if (broadcastProduction.getUsername() == null) {
                        addToMap(viettelThisMonthMap, broadcastProduction);
                    } else {
                        addToMap(viettelThisMonthUnknownMap, broadcastProduction);
                    }
                }
            }
        } catch (Exception ex) {
            logger.error("", ex);
        }
        logger.trace("Done Initializing {}", this.getClass().getSimpleName());
    }

    private void addToMap(Map<String, UserBroadcastAndProductionModel> map, BroadcastProduction broadcastProduction) {
        String username = broadcastProduction.getUsername();
        if (username == null) {
            username = "Unknown";
        }
        if (!map.containsKey(username)) {
            map.put(username, new UserBroadcastAndProductionModel(username));
        }
        map.get(username).addBroadcastProduction(broadcastProduction);
    }

    public class UserBroadcastAndProductionModel {

        String username;
        List<BroadcastProduction> broadcastProductions = new ArrayList<BroadcastProduction>();

        public UserBroadcastAndProductionModel(String username) {
            this.username = username;
        }

        public void addBroadcastProduction(BroadcastProduction broadcastProduction) {
            broadcastProductions.add(broadcastProduction);
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public List<BroadcastProduction> getBroadcastProductions() {
            return broadcastProductions;
        }

        public void setBroadcastProductions(List<BroadcastProduction> broadcastProductions) {
            this.broadcastProductions = broadcastProductions;
        }
    }

    public Collection<UserBroadcastAndProductionModel> getMobifoneYesterdayList() {
        return new ArrayList<UserBroadcastAndProductionModel>(mobifoneYesterdayMap.values());
    }

    public Collection<UserBroadcastAndProductionModel> getVinaphoneYesterdayList() {
        return new ArrayList<UserBroadcastAndProductionModel>(vinaphoneYesterdayMap.values());
    }

    public Collection<UserBroadcastAndProductionModel> getViettelYesterdayList() {
        return new ArrayList<UserBroadcastAndProductionModel>(viettelYesterdayMap.values());
    }

    public Collection<UserBroadcastAndProductionModel> getMobifoneThisMonthList() {
        return new ArrayList<UserBroadcastAndProductionModel>(mobifoneThisMonthMap.values());
    }

    public Collection<UserBroadcastAndProductionModel> getVinaphoneThisMonthList() {
        return new ArrayList<UserBroadcastAndProductionModel>(vinaphoneThisMonthMap.values());
    }

    public Collection<UserBroadcastAndProductionModel> getViettelThisMonthList() {
        return new ArrayList<UserBroadcastAndProductionModel>(viettelThisMonthMap.values());
    }

    public Collection<UserBroadcastAndProductionModel> getMobifoneYesterdayUnknownList() {
        return new ArrayList<UserBroadcastAndProductionModel>(mobifoneYesterdayUnknownMap.values());
    }

    public Collection<UserBroadcastAndProductionModel> getVinaphoneYesterdayUnknownList() {
        return new ArrayList<UserBroadcastAndProductionModel>(vinaphoneYesterdayUnknownMap.values());
    }

    public Collection<UserBroadcastAndProductionModel> getViettelYesterdayUnknownList() {
        return new ArrayList<UserBroadcastAndProductionModel>(viettelYesterdayUnknownMap.values());
    }

    public Collection<UserBroadcastAndProductionModel> getMobifoneThisMonthUnknownList() {
        return new ArrayList<UserBroadcastAndProductionModel>(mobifoneThisMonthUnknownMap.values());
    }

    public Collection<UserBroadcastAndProductionModel> getVinaphoneThisMonthUnknownList() {
        return new ArrayList<UserBroadcastAndProductionModel>(vinaphoneThisMonthUnknownMap.values());
    }

    public Collection<UserBroadcastAndProductionModel> getViettelThisMonthUnknownList() {
        return new ArrayList<UserBroadcastAndProductionModel>(viettelThisMonthUnknownMap.values());
    }
}
